define(['angular', 'app.directives','services/user/user_service','controllers/user/user'], function (angular, directives) {
    'use strict';

    directives.directive("adminLogin", ['$q','$location','$stateParams','$uibModal','curuserService', function ($q,$location,$stateParams,$uibModal,curuserService) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: function () {
                return "/app/partial/user/login.html";
            },
            link: function ($scope) {
                var a=$(window).height();//背景图
                $(".all").height(a);//背景图


                $scope.loginSubmit=function(){
                    //console.info( $location.search().ref);
                    curuserService.login($scope.username,$scope.password,$location.search().ref,function(data){
                        location.href="/jewellery";
                    },function(data){
                        alert(data.message);
                    });
                }
            }
        }
    }]);
    directives.directive("userRegister", ['$q','$http','$location','$stateParams','$uibModal','curuserService', function ($q,$http,$location,$stateParams,$uibModal,curuserService) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: function () {
                return "/app/partial/user/register.html";
            },
            link: function ($scope) {
                var error=function(err){
                    alert(err.message);
                };
                curuserService.getXy("yhxy",function(data){
                    $scope.xy = data.data;
                }, function(err){
                    alert(err.message);
                });

                $scope.lookXy=function(xy){
                    $uibModal.open({
                        animation: true,
                        size:"lg",
                        backdrop:'static',
                        templateUrl: '/app/partial/user/look_xy.html',
                        controller: "lookXyController",
                        resolve: {
                            model:{xy:xy}
                        }
                    });
                }
                var userName=$location.search().userName;
                var registCode=$location.search().registCode;
                if(registCode!=undefined&&registCode!=null&&registCode!=""&&userName!=undefined&&userName!=null&&userName!=""){
                    var regInfo = {
                        userName: userName,
                        registCode: registCode
                    };
                    curuserService.getUserInfoByRegCode(regInfo,function(data){
                        var chStatus= data.data.checkStatus;
                        if(chStatus=='0'){
                            alert('暂未审核请耐心等待');
                        }else if(chStatus=='1'){//审核通过
                            alert('审核通过，可直接登录系统');
                        }else if(chStatus=='2'){//2
                            alert('审核未通过，请修改注册信息继续注册');
                        }else{
                            alert('用户名与注册码不匹配');
                        }
                        $scope.user=data.data;
                    },function(data){
                        alert('查询失败，请稍后再试！！');
                    })
                }else{
                    $scope.user={};
                }

                //获取验证码
                $scope.getYZM=function(){
                    if($scope.user.phone==undefined||$scope.user.phone==""){
                       alert("请输入手机号");
                        return false;
                    }
                    curuserService.getYZM($scope.user.phone,function(data){
                        $scope.yzmNumber=data.data;
                        if(data.status=='1'){
                            alert("已发送")
                        }else{
                            alert("发送失败")
                        }
                    },error)
                }
                /**
                 * 新用户注册
                 **/
                var reg = function(regInfo) {
                    curuserService.registerUser(regInfo,function(data){
                        alert('注册成功，您的注册码为'+data.data+" 请耐心等待审核");
                        location.href="/jewellery";
                    },function(data){
                        alert('注册失败，请重新注册');
                    })

                };
                var regUpdate=function(regInfo){
                    curuserService.registerUserUpdate(regInfo,function(data){
                        alert("修改成功");
                        location.href="/jewellery";
                    },function(data){
                        alert('修改失败');
                    })
                }
                var uploadFile=function (formData) {
                    $http({
                        method:'POST',
                        url:"/dapi/upload/link",
                        data: formData,
                        headers: {'Content-Type':undefined},
                        transformRequest: angular.identity
                    }).success(function(data){
                        $scope.pic_path=data.data;
                        if ($scope.password_confirm != $scope.user.password) {
                            alert('密码两次输入不一致');
                            return;
                        }
                        if ($scope.user.phone==undefined||$scope.user.phone=="") {
                            alert('请输入手机号码');
                            return;
                        }
                        if ($scope.yzm==undefined||$scope.yzm=="") {
                            alert('请先输入验证码');
                            return;
                        }
                        if ($scope.yzmNumber!=$scope.yzm) {
                            alert('验证码输入错误');
                            return;
                        }
                        if ($scope.user.agentCompany==undefined||$scope.user.agentCompany=="") {
                            alert('请选择代理公司');
                            return;
                        }
                        if ($scope.pic_path==undefined||$scope.pic_path=="") {
                            alert('请先上传营业执照');
                            return;
                        }else{
                            $scope.user.yyzz=$scope.pic_path;
                        }
                        if(registCode!=null&&registCode!=''){
                            regUpdate($scope.user);
                        }else{
                            reg($scope.user);
                        }

                    });


                };
                //注册
                $scope.registerBtn=function(){
                    var formData = new FormData();
                    if(document.querySelector('input[type="file"]').files[0]!=null){
                        formData.append("file",document.querySelector('input[type="file"]').files[0]);
                    }else{
                        alert("请选择文件");
                        return false;
                    }
                    uploadFile(formData);
                }
                //获取代理商列表
                $http({
                    method:'GET',
                    url:"/dapi/agent/getList",
                    headers: {'Content-Type':undefined},
                    transformRequest: angular.identity
                }).success(function(data){
                    $scope.agentList=data.data;
                });
                $scope.userNamechange=function(){
                    document.querySelector('input[name="userName"]').value=document.querySelector('input[name="userName"]').value.replace(/[\W]/g,'');
                }
            }
        }
    }]);

    directives.directive("userForgetPwd", ['$q','$location','$stateParams','$uibModal','curuserService', function ($q,$location,$stateParams,$uibModal,curuserService) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: function () {
                return "/app/partial/user/forgetPwd.html";
            },
            link: function ($scope) {


                $scope.nextSubmit=function(){
                    if($scope.yzm==null||$scope.yzm==''){
                        alert("请输入验证码");
                        return;
                    }
                    if($scope.yzm!=$scope.yzmNumber){
                        alert("验证码不正确");
                        return;
                    }
                    location.href="/user/changePwd/index?username="+$scope.username+"&phone="+$scope.phone;
                };

                $scope.getRegisterYzm=function(){

                    curuserService.justPhoneAndName($scope.username,$scope.phone,function(data){
                        if(data.data==1){
                            curuserService.getYZM($scope.phone,function(data){
                                $scope.yzmNumber=data.data;
                                if(data.status=='1'){
                                    alert("已发送")
                                }else{
                                    alert("发送失败")
                                }
                            },function(data){
                                alert(data.message);
                            })
                        }else{
                            alert("用户名或手机号错误");
                        }
                    },function(data){
                        alert(data.message);
                    });
                }
            }
        }
    }]);



    directives.directive("userChangePwd", ['$q','$location','$stateParams','$uibModal','curuserService', function ($q,$location,$stateParams,$uibModal,curuserService) {
        return {
            restrict: 'E',
            scope: {},
            replace: true,
            templateUrl: function () {
                return "/app/partial/user/changePwd.html";
            },
            link: function ($scope) {
                var username=$location.search().username;
                var phone=$location.search().phone;

                $scope.changePwd=function(){
                    if($scope.password==null||$scope.password==""){
                        alert("密码不能为空");
                        return;
                    }
                    if($scope.password!=$scope.newPwd){
                        alert("两次密码输入不一致");
                        return;
                    }
                    curuserService.changePwd(username,phone,$scope.password,function(data){
                        if(data.data==1){
                            alert("重置成功");
                            location.href="/user/login/index?ref=%2Fxdd";
                        }else {
                            alert("重置失败");
                        }
                    },function(data){
                        alert(data.message);
                    });
                }
            }
        }
    }]);
});